package com.jstyle.jclifexd.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;

import com.jstyle.jclifexd.NetWork.NetWorkUtil;
import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.adapter.RecordAdapter;
import com.jstyle.jclifexd.daoManager.UserInfoDaoManager;
import com.jstyle.jclifexd.model.RootRecord;
import com.jstyle.jclifexd.utils.DateUtil;
import com.jstyle.jclifexd.utils.ResolveData;
import com.jstyle.jclifexd.utils.Utils;

import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 成长记录和成绩
 */
public class GrowthMianRecordActivity extends BaseActivity {
    @BindView(R.id.title)
    Button title;

    @BindView(R.id.start_time)
    TextView start_time;
    @BindView(R.id.step_dayvalue)
    TextView step_dayvalue;

    @BindView(R.id.time_value)
    TextView time_value;
    @BindView(R.id.half_value)
    TextView half_value;
    @BindView(R.id.full_value)
    TextView full_value;
    @BindView(R.id.lichen_value)
    TextView lichen_value;

    @BindView(R.id.my_chengji)
    RadioButton my_chengji;
    @BindView(R.id.record)
    RadioButton record;
    @BindView(R.id.record_RecyclerView)
    RecyclerView record_RecyclerView;
    LinearLayoutManager linearLayoutManager;
    String uid;
    String uname;
    boolean ismyself = false;
    RecordAdapter recordAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_growthrecord);
        ButterKnife.bind(this);
        linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setAutoMeasureEnabled(true);
        recordAdapter = new RecordAdapter(this);
        record_RecyclerView.setHasFixedSize(false);
        record_RecyclerView.setLayoutManager(linearLayoutManager);
        record_RecyclerView.setAdapter(recordAdapter);
        if (null != getIntent()) {
            uid = getIntent().getStringExtra("uid");
            uname = getIntent().getStringExtra("uname");
            if (null != getIntent().getStringExtra("my")) {
                ismyself = true;
                title.setText(UserInfoDaoManager.getUserByUid(NetWorkUtil.getUserId()).getName());
                my_chengji.setText(getResources().getString(R.string.grades));
            } else {
                my_chengji.setText(getResources().getString(R.string.performance));
                title.setText(uname);
            }

            if(!Utils.isZh(GrowthMianRecordActivity.this)){
                my_chengji.setTextSize(12);
            }

            fingyGrade();
        }


    }


    private void fingyGrade() {
        Map<String, Object> map = new HashMap<>();
        map.put("uid", uid);
        NetWorkUtil.getInstance().getJstyleApi().queryGrade(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<RootRecord>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @SuppressLint("SetTextI18n")
            @Override
            public void onNext(RootRecord rootRecord) {
                if (null != rootRecord.getData()) {
                    if(Utils.isZh(GrowthMianRecordActivity.this)){
                        start_time.setText(rootRecord.getData().getStart()+" 开始运动");
                    }else{
                        start_time.setText(getResources().getString(R.string.startsrun)+rootRecord.getData().getStart());
                    }
                    step_dayvalue.setText(rootRecord.getData().getDays() + getResources().getString(R.string.day));
                    NumberFormat numberFormat = ResolveData.getNumberFormat(2);
                    lichen_value.setText(numberFormat.format(rootRecord.getData().getMileage()) + " "+getResources().getString(R.string.distance));
                    time_value.setText(DateUtil.getRankTimeer((int) rootRecord.getData().getDuration(), GrowthMianRecordActivity.this));
                    half_value.setText(rootRecord.getData().getHalfMarathon() + getResources().getString(R.string.Times));
                    full_value.setText(rootRecord.getData().getMarathon() + getResources().getString(R.string.Times));
                    recordAdapter.updateData(rootRecord.getData().getGrade());
                }

            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onComplete() {
            }
        });
    }

    @OnClick({R.id.iv_back, R.id.my_chengji, R.id.record})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.my_chengji:

                break;
            case R.id.record:
                Intent intent = new Intent(this, GrowthRecordActivity.class);
                if (ismyself) {
                    intent.putExtra("my", "my");//my代表自己查看的数据
                }
                intent.putExtra("uid", uid);
                intent.putExtra("uname", uname);
                startActivity(intent);
                break;

        }
    }
}
